<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2024/12/18 0018
  Time: 10:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div id="bodyBox">
    <form id="SearchFm" method="post" action="">
        <table id="tbl">
            <tr>
                <td class="col1">请选择产品I级分类</td>
                <td class="col2"><select id="firstLevel" name="firstKindId"><option value="0">请选择</option></select></td>
            </tr>
            <tr>
                <td class="col1">请选择产品II级分类</td>
                <td class="col2"><select id="secondLevel" name="secondKindId"><option value="0">请选择</option></select></td>
            </tr>
            <tr>
                <td class="col1">请选择产品III级分类</td>
                <td class="col2"><select id="thirdLevel" name="secondKindId"><option value="0">请选择</option></select></td>
            </tr>
            <tr>
                <td class="col1">请选择产品产品用途类型</td>
                <td class="col2"><select  id="use" name="type"><option value="0">请选择</option></select></td>
            </tr>
            <tr>
                <td class="col1">请选择建档时间</td>
                <td class="col2"><input type="date" name="startDate"/>至<input type="date" name="endDate"/></td>
            </tr>
        </table>
    </form>
</div>
</div>
<script type="text/javascript" src="/js/jquery-3.7.0.min.js"></script>
<script type="text/javascript" src="/bootstrap/bootstrap-5.3.3-dist/js/bootstrap.bundle.min.js"></script>
<script type="text/javascript" src="/js/common.js"></script>
<script type="text/javascript">
    $(function () {
        // 初始化数据
        initData();
        // 初始化事件
        initEvent();
    });
    function initData() {
        // 初始化一级分类
        initialFirstLevel();
        // 初始化使用类型
        initialUse();
    }
    function initEvent() {
        // 一级分类选择事件
        $('#firstLevel').on('change', function () {
            let firstId = $(this).val();
            if (firstId == '') {
                $('#secondLevel option:gt(0)').remove();
                $('#thirdLevel option:gt(0)').remove();
                return;
            }
            // 存储一级分类名称
            $('#firstKindName').val($(this).find('option:selected').text());
            // 初始化二级分类
            initialSecondLevel(firstId);
        });
        // 二级分类选择事件
        $('#secondLevel').on('change', function () {
            let secondId = $(this).val();
            if (secondId == '') {
                $('#thirdLevel option:gt(0)').remove();
                return;
            }
            // 存储二级分类名称
            $('#secondKindName').val($(this).find('option:selected').text());
            // 存储二级分类名称
            initialThirdLevel(secondId);
        });
        // 三级分类选择事件
        $('#thirdLevel').on('change', function () {
            let thirdId = $(this).val();
            if (thirdId == '') {
                return;
            }
            // 存储三级分类名称
            $('#thirdKindName').val($(this).find('option:selected').text());
        });
        // 产品查询点击事件
        $('#productQueryBtn').on('click', function () {
            AddSessionStorage();
            window.location.href = "/d-file/productArchivesQuery";
        })

        $('#formulateMaterialQueryBtn').on('click', function () {
            AddSessionStorage();
            window.location.href = "/d-file/formulateDesignComposition";
        })


    }
    function initialFirstLevel() {
        $.ajax({
            url: "/d-level-first/initialLevel",
            type: "get",
            dataType: "json",
            success: function (data) {
                console.log(data);
                let firstLevel = $('#firstLevel');
                $('#firstLevel option:gt(0)').remove();
                $('#secondLevel option:gt(0)').remove();
                $('#thirdLevel option:gt(0)').remove();
                for (let i = 0; i < data.length; i++) {
                    let option = $('<option>');
                    option.val(data[i].firstId);
                    option.text(data[i].firstName);
                    firstLevel.append(option);
                }
            }
        })
    }
    function initialSecondLevel(firstId) {
        $.ajax({
            url: "/d-level-second/initialLevel?firstId="+firstId,
            type: "get",
            dataType: "json",
            success: function (data) {
                console.log(data);
                let secondLevel = $('#secondLevel');
                $('#secondLevel option:gt(0)').remove();
                $('#thirdLevel option:gt(0)').remove();
                for (let i = 0; i < data.length; i++) {
                    let option = $('<option>');
                    option.val(data[i].secondId);
                    option.text(data[i].secondName);
                    secondLevel.append(option);
                }
            }
        })
    }
    function initialThirdLevel(secondId) {
        $.ajax({
            url: "/d-level-third/initialLevel?secondId="+secondId,
            type: "get",
            dataType: "json",
            success: function (data) {
                console.log(data);
                let thirdLevel = $('#thirdLevel');
                $('#thirdLevel option:gt(0)').remove();
                for (let i = 0; i < data.length; i++) {
                    let option = $('<option>');
                    option.val(data[i].thirdId);
                    option.text(data[i].thirdName);
                    thirdLevel.append(option);
                }
            }
        })
    }
    function initialUse() {
        $.ajax({
            url: "/d-product-use/initialUse",
            type: "get",
            dataType: "json",
            success: function (data) {
                console.log(data);
                let use = $('#use');
                $('#use option:gt(0)').remove();
                for (let i = 0; i < data.length; i++) {
                    let option = $('<option>');
                    option.val(data[i].useId);
                    option.text(data[i].useName);
                    use.append(option);
                }
            }
        })
    }
    // 存储查询条件
    function AddSessionStorage(){
        let firstId = $('#firstLevel').val();
        let secondId = $('#secondLevel').val();
        let thirdId = $('#thirdLevel').val();
        let useId = $('#use').val();
        let startDate = $('input[name="startDate"]').val();
        let endDate = $('input[name="endDate"]').val();
        new Date(startDate)
        new Date(endDate)
        if (startDate > endDate) {
            alert('开始时间不能大于结束时间');
            return;
        }
        sessionStorage.setItem('firstId', firstId);
        sessionStorage.setItem('secondId', secondId);
        sessionStorage.setItem('thirdId', thirdId);
        sessionStorage.setItem('useId', useId);
        sessionStorage.setItem('startDate', startDate);
        sessionStorage.setItem('endDate', endDate);
    }
</script>
</body>
</html>

